import Vuex from "vuex"
import Vue from "vue"
import {requestByGet, requestByPost} from "@/utils/request";

Vue.use(Vuex);
export default new Vuex.Store({
    state: {
        userInfo: {
            "id": "1300412843635351553",
            "username": "15527230936",
            "realname": "vijay",
            "avatar": "https://image.welian.com/fnem1584694484269_288_288_17.png?x-oss-process=image/resize,m_fill,h_100,w_100",
            "birthday": null,
            "sex": 1,
            "email": "",
            "phone": "15527230936",
            "orgCode": null,
            "orgCodeTxt": null,
            "status": 1,
            "delFlag": 0,
            "workNo": null,
            "post": null,
            "telephone": null,
            "createBy": null,
            "createTime": "2020-08-31 20:39:17",
            "updateBy": null,
            "updateTime": null,
            "activitiSync": null,
            "userIdentity": null,
            "departIds": null,
            "thirdType": null,
            "relTenantIds": null,
            "role": null,
            "companyName": null,
            "profession": null,
            "idCard": null,
            "politic": null,
            "provinceCode": null,
            "cityCode": null,
            "areaCode": null,
            "businessAddress": null,
            "incubator": null,
            "college": null,
            "goodCoaching": null,
            "personal": null,
            "profile": null,
            "tutorial": null,
            "teaching": null,
            "investment": null,
            "entrepreneur": null,
            "roleType": "1",
            "teacherTime": null,
            "onJob": null,
            "userType": "3",
            "adminType": "0"
        }
    },
    mutations: {
        UPDATE_USER_INFO(state, data) {
            state.userInfo = data;
        }
    },
    actions: {
        async login({commit}, params) {
            const {phone: username, captcha} = params;
            const {token, userInfo} = await requestByPost(`/sys/appLogin`, {captcha, username});
            sessionStorage.setItem("X-Access-Token", token);
            sessionStorage.setItem("phone", userInfo.phone);
            sessionStorage.setItem("userId", userInfo.id);
            commit("UPDATE_USER_INFO", userInfo);
        },
        async initUser({commit}) {
            const userInfo = await requestByGet(`sys/user/queryById`, {id: sessionStorage.getItem("userId")});
            commit("UPDATE_USER_INFO", userInfo);
        }
    }
});
